home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4844 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.1 KB  |  127 lines

  1. Path: news.bellglobal.com!stupy
  2. From: stupy@freenet.durham.org (Steve Tupy)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: Pascal 'real' to C 'float'
  5. Followup-To: comp.lang.c,comp.lang.c++
  6. Date: 28 Jan 1996 22:52:20 GMT
  7. Organization: Durham Free-Net
  8. Message-ID: <4egun4$kc9@news.bellglobal.com>
  9. References: <4e674d$ett@maestro.netinc.ca>
  10. NNTP-Posting-Host: 204.101.165.17
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Shawn Wallace (swallace@netinc.ca) wrote:
  14. : I've run into a little difficulty converting the data from an older
  15. : Pascal program into C.
  16.  
  17. : I need to convert the pascal 'real' number (which I believe to be a
  18. : 6-byte struct) to a C/C++ float or double (which are 4 and 8-bytes
  19. : respecively).
  20.  
  21. Try these out, they work just fine but are "C", not C++... for that I
  22. apologize...
  23.  
  24.     Please do not leave "char burns" on my butt for posting this.. I am
  25. new to newsgroups and do not yet have total control over this editor!
  26.  
  27. <<<<<<<<<<<<<<<<<<<<<<<<<<<<< Begin REAL.H >>>>>>>>>>>>>>>>>>>>>>>>>>>>
  28.  
  29. #ifndef _REAL_H
  30. #define _REAL_H
  31. extern "C" {
  32. typedef unsigned char pasreal[6];
  33. typedef unsigned char bassngl[4];
  34. typedef unsigned char basdble[8];
  35. typedef union {
  36.           double        value;
  37.           unsigned char byte[8];
  38.         } IEEEdouble;
  39.  
  40. double pasrealtodouble(pasreal OldNum);
  41. double bassngltodouble(bassngl OldNum);
  42. double basdbletodouble(basdble OldNum);
  43.  
  44. void doubletopasreal(pasreal *New, double Old);
  45. void doubletobassngl(bassngl *New, double Old);
  46. void doubletobasdble(basdble *New, double Old);
  47. };
  48. #endif
  49. <<<<<<<<<<<<<<<<<<<<<<< End REAL.H >>>>>>>>>>>>>>>>>>>>>>>>>>
  50.  
  51. <<<<<<<<<<<<<<<<<<<<<<< Begin REAL.C >>>>>>>>>>>>>>>>>>>>>>>>
  52.  
  53. #include "real.h"
  54.  
  55. /*  converts TP 6-byte real to IEEE 8-byte real  */
  56.  
  57. double pasrealtodouble(pasreal OldNum) {
  58.  
  59.   IEEEdouble NewNum;
  60.   char       Sign;
  61.   int        Exp;
  62.   int        X;
  63.  
  64.   for(X = 0; X < 2; X++)
  65.     NewNum.byte[X] = 0x00;
  66.  
  67.   Sign = OldNum[5] & 0x80;
  68.   Exp = OldNum[0] - 0x81 + 0x3FF;
  69.   NewNum.byte[6] = (Exp << 4);
  70.   NewNum.byte[7] = (Exp >> 4) | Sign;
  71.  
  72.   for(X = 5; X > 1; X--) {
  73.     OldNum[X] <<= 1;
  74.     OldNum[X] |= OldNum[X-1] >> 7;
  75.   }
  76.   OldNum[1] <<= 1;
  77.  
  78.   for(X = 6; X >= 2; X--) {
  79.     NewNum.byte[X] |= OldNum[X-1] >> 4;
  80.     NewNum.byte[X-1] =  OldNum[X-1] << 4;
  81.   }
  82.  
  83.   return(NewNum.value);
  84. }
  85.  
  86. /*  converts IEEE 8-byte real to pascal 6-byte real  */
  87.  
  88. void doubletopasreal(pasreal *New, double Old) {
  89.   char    Sign;
  90.   int     Exp;
  91.   int     X;
  92.   unsigned char *NewNum, *OldNum;
  93.  
  94.   NewNum = (char *) New;
  95.   OldNum = (char *) &Old;
  96.  
  97.   Sign = OldNum[7] & 0x80;
  98.   Exp  = ((OldNum[7] & 0x7F) << 4) + (OldNum[6] >> 4) + 0x81 - 0x3FF;
  99.  
  100.   for (X = 5; X >= 1; X--) {
  101.     NewNum[X] = OldNum[X+1] << 4;
  102.     NewNum[X] |= OldNum[X] >> 4;
  103.   }
  104.  
  105.   for (X = 1; X < 5; X++) {
  106.     NewNum[X] >>= 1;
  107.     NewNum[X] |= NewNum[X+1] << 7;
  108.   }
  109.   NewNum[5] >>= 1;
  110.  
  111.   NewNum[5] |=  Sign;
  112.   NewNum[0] =   Exp;
  113. }
  114. <<<<<<<<<<<<<<<<<<<<<<<<<<<< End REAL.H >>>>>>>>>>>>>>>>>>>>>>>>>>>>
  115.  
  116. This should compile ok, I use it with Borland, so you might have to adapt it
  117. for other compilers but it is all basically there and in straight "C"...
  118.  
  119. To others, please, no flames about off topic posting, It is useful code
  120. under ANY format.
  121.  
  122. Take care!
  123.  
  124. Steve
  125.  
  126.                       
  127.